﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Services;
using SmObjects;
using SuperORM;
using System.Data;
using System.Reflection;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Services;


    /// <summary>
    /// Core 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [ScriptService]
    public class Core : System.Web.Services.WebService
    {

        public Core()
        {

            //如果使用设计的组件，请取消注释以下行 
            //InitializeComponent();
        }
        [WebMethod(EnableSession = true, Description = "分页加载数据")]
        public string GetSmPageData(string DBAlias, string tablename, string key, string selectfield, string filter, string orderby, int orderbytype, int pagesize, int pageindex)
        {
            try
            {
                string pagejson = "";
                SmPageData data = new SmPageData();
                ISession session = Session["session"] as ISession;
                if (session != null)
                {
                    SmQuery qry = session.CreateSmQuery(DBAlias);
                    SmQueryParam qp = new SmQueryParam();
                    qp.TableName = tablename;
                    qp.Key = key;
                    qp.SelectField = selectfield;
                    qp.Where = filter;
                    qp.Orderbyid = orderby;
                    if (orderbytype == 0)
                    {
                        qp.OrderType = OrderType.ASC;
                    }
                    else if (orderbytype == 99) //仅矿山地质环境采集使用
                    {
                        int rc2 = 0;
                        int row = (pageindex - 1) * pagesize;
                        string sql = "select * from (select " + selectfield +
                            " from " + tablename +
                            " where " + filter +
                            " order by " + orderby +
                            " asc ) where rownum <= " + pagesize +
                            " and 许可证号 not in(select * from (select 许可证号 from " + tablename +
                            " where " + filter +
                            " order by " + orderby +
                            " asc ) where rownum <= " + row + ")";
                        string rowSql = " select " + selectfield +
                            " from " + tablename +
                            " where " + filter;
                        DataTable tbl2 = qry.GetDataTableBySQL(sql);
                        DataTable tblRw = qry.GetDataTableBySQL(rowSql);
                        rc2 = tblRw.Rows.Count;
                        data.Key = key;
                        data.PageIndex = pageindex;
                        data.PageSize = pagesize;
                        data.RecordCount = rc2;
                        data.FromDataTable(tbl2);
                        pagejson = SmPageData.Serialize(data);
                        return pagejson;
                    }
                    else
                    {
                        qp.OrderType = OrderType.DESC;
                    }
                    qp.PageSize = pagesize;
                    qp.PageIndex = pageindex;

                    int rc = 0;
                    DataTable tbl = qry.GetFYTableBySQL(qp, out rc);
                    data.Key = key;
                    data.PageIndex = pageindex;
                    data.PageSize = pagesize;
                    data.RecordCount = rc;
                    data.FromDataTable(tbl);
                    pagejson = SmPageData.Serialize(data);
                }
                return pagejson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "加载数据项")]
        public string GetSmObjectData(string DBAlias, string tablename, string key, int id)
        {
            try
            {

                ISession session = Session["session"] as ISession;
                SmObjectData obj = new SmObjectData();
                SmQuery qry = session.CreateSmQuery(DBAlias);
                DataTable tbl = qry.GetDataTableBySQL("select * from " + tablename + " where " + key + "=" + id);
                if (tbl != null && tbl.Rows.Count > 0)
                {
                    obj.ID = id;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), SmObjectData.TranslateObjectToString(tbl.Rows[0][i]));
                    }
                }
                else if (tbl != null)
                {
                    obj.ID = -1;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), "");
                    }

                }
                return SmObjectData.Serialize(obj);
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "加载第一个数据项 作者：刘树峰")]
        public string GetFirstSmObject(string dbalias, string tablename, string condition)
        {
            try
            {
                ISession session = Session["session"] as ISession;
                SmObjectData obj = new SmObjectData();
                obj.DBAlias = dbalias;
                obj.TableName = tablename;
                SmQuery qry = session.CreateSmQuery(dbalias);
                DataTable tbl = null;
                if (condition == "") condition = "1=1";
                string driver = session.getDatasource(dbalias).DataDriverType;
                if (driver == "SuperORM.Driver.OracleClientDriver")
                {
                    tbl = qry.GetDataTableBySQL("select * from " + tablename + " where rownum<=1 and " + condition);
                }
                else
                {
                    tbl = qry.GetDataTableBySQL("select top 1 * from " + tablename + " where " + condition);
                }
                if (tbl != null && tbl.Rows.Count > 0)
                {
                    obj.ID = 1;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), SmObjectData.TranslateObjectToString(tbl.Rows[0][i]));
                    }
                }
                else if (tbl != null)
                {
                    obj.ID = -1;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), "");
                    }

                }
                session.Close();
                return SmObjectData.Serialize(obj);
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "根据条件加载数据项")]
        public string GetSmObject(string dbalias, string tablename, string condition, string lstablename)
        {
            try
            {

                ISession session = Session["session"] as ISession;
                SmObjectData obj = new SmObjectData();
                SmQuery qry = session.CreateSmQuery(dbalias);
                //DataTable tbl = qry.GetDataTableBySQL("select top 1 * from " + tablename + " where " + condition);
                DataTable tbl = null;
                if (condition == "") condition = "1=1";
                string driver = session.getDatasource(dbalias).DataDriverType;
                if (driver == "SuperORM.Driver.OracleClientDriver")
                {
                    tbl = qry.GetDataTableBySQL("select  * from " + tablename + " where rownum<=1 and " + condition);
                }
                else
                {
                    tbl = qry.GetDataTableBySQL("select top 1 * from " + tablename + " where " + condition);
                }

                if (tbl != null && tbl.Rows.Count > 0)
                {
                    obj.ID = 1;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), SmObjectData.TranslateObjectToString(tbl.Rows[0][i]));
                    }
                    obj.DBAlias = dbalias;
                    obj.TableName = lstablename;
                }
                else if (tbl != null)
                {
                    obj.ID = -1;
                    for (int i = 0; i < tbl.Columns.Count; i++)
                    {
                        obj.Data.Add(tbl.Columns[i].ColumnName.ToUpper(), "");
                    }
                    obj.DBAlias = dbalias;
                    obj.TableName = lstablename;
                }
                return SmObjectData.Serialize(obj);
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "业务：办理完成")]
        public string BLWC(string project_id, string xmlx)
        {
            //ISession session = Session["session"] as ISession;
            //if (session != null)
            //{
            //    采矿申请登记[] m_cksqdjs = session.LoadEx2<采矿申请登记>("select * from 采矿申请登记 where PROJECT_ID='" + project_id + "'", "CKQDJK");
            //    if (m_cksqdjs != null && m_cksqdjs.Length > 0)
            //    {
            //        采矿申请登记 m_cksqdj = m_cksqdjs[0];
            //        string xkzh = SmObjectData.TranslateObjectToString(m_cksqdj.XKZH);
            //        if (xkzh == "")
            //        {
            //            return "请先申请配号";
            //        }
            //        if (xmlx == "YX")
            //        {
            //            m_cksqdj.项目类型 = 1030;
            //            session.Update(m_cksqdj);
            //            session.Flush();

            //            string yxkzh = SmObjectData.TranslateObjectToString(m_cksqdj.原许可证号);
            //            string ydjs = GetSmObject(dbalias, tablename, "XKZH =" + yxkzh, lstablename);
            //            string ls = UpdateSmObject(ydjs);
            //            session.Delete(typeof(采矿申请登记), "XKZH = '" + yxkzh + "'");
            //            session.Flush();
            //            return ls;
            //        }
            //        else if (xmlx == "ZX")
            //        {
            //            m_cksqdj.项目类型 = 1070;
            //            session.Update(m_cksqdj);
            //            session.Flush();

            //            string yxkzh = SmObjectData.TranslateObjectToString(m_cksqdj.原许可证号);
            //            string ydjs = GetSmObject("CKQDJK", "采矿申请登记", "XKZH =" + yxkzh, "CKS_LSCKSQDJ");
            //            string ls = UpdateSmObject(ydjs);
            //            session.Delete(typeof(采矿申请登记), "XKZH = '" + yxkzh + "'");
            //            session.Flush();

            //            ydjs = GetSmObject("CKQDJK", "采矿申请登记", "PROJECT_ID =" + project_id, "CKS_LSCKSQDJ");
            //            ls = UpdateSmObject(ydjs);
            //            session.Delete(typeof(采矿申请登记), "PROJECT_ID = '" + project_id + "'");
            //            session.Flush();
            //            return ls;
            //        }
            //    }
            //}
            return "未办理完成";
        }

        [WebMethod(EnableSession = true, Description = "加载数据表")]
        public string GetSmDataTable(string dbalias, string sql)
        {
            try
            {
                string pagejson = "";
                SmDataTable data = new SmDataTable();
                ISession session = Session["session"] as ISession;
                if (session != null)
                {
                    SmQuery qry = session.CreateSmQuery(dbalias);
                    DataTable tb = qry.GetDataTableBySQL(sql);
                    data.FromDataTable(tb);
                    pagejson = SmDataTable.Serialize(data);
                }
                return pagejson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "SmCommand")]
        public string SmSubmit(string dbalias, string sql)
        {
            try
            {
                string result = "操作成功";
                SmDataTable data = new SmDataTable();
                ISession session = Session["session"] as ISession;
                if (session != null)
                {
                    SmCommand smCommand = session.CreateCommand(dbalias);
                    smCommand.CommandType = CommandType.Text;
                    smCommand.CommandText = sql;
                    smCommand.ExecuteNonQuery();
                }
                return result;
            }
            catch (Exception ex)
            {
                return "操作失败";
            }
        }

        [WebMethod(EnableSession = true, Description = "联想")]
        public string IntelligentQuery(string dbalias, string text)
        {
            try
            {
                string pagejson = "";
                SmDataTable data = new SmDataTable();
                ISession session = Session["session"] as ISession;
                if (dbalias == "CKQDJK")
                {
                    if (session != null)
                    {
                        SmQuery qry = session.CreateSmQuery(dbalias);
                        string sql = "select * from (select * from (select * from (select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '" +
                            text + "%' union select xkzh as val  from 采矿申请登记 where xkzh like '" + text + "%' union select " +
                            "矿山名称 as val  from 采矿申请登记 where 矿山名称 like '" + text.ToUpper() + "%' union select xkzh as val  from " +
                            "采矿申请登记 where xkzh like '" + text.ToUpper() + "%' union select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '" +
                            text.ToLower() + "%' union select xkzh as val  from 采矿申请登记 where xkzh like '" + text.ToLower() +
                            "%')) order by sys_guid())where ROWNUM<=5";
                        DataTable tb = qry.GetDataTableBySQL(sql);
                        int count = tb.Rows.Count;
                        if (count != 5)
                        {
                            DataTable tb2 = qry.GetDataTableBySQL("select * from (select * from (select * from (select 矿山名称 as val " +
                            "from 采矿申请登记 where 矿山名称 like '%" +
                            text + "%' union select xkzh as val  from 采矿申请登记 where xkzh like '%" + text + "%' union select " +
                            "矿山名称 as val  from 采矿申请登记 where 矿山名称 like '%" + text.ToUpper() + "%' union select xkzh as val  from " +
                            "采矿申请登记 where xkzh like '%" + text.ToUpper() + "%' union select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '%" +
                            text.ToLower() + "%' union select xkzh as val  from 采矿申请登记 where xkzh like '%" + text.ToLower() +
                            "%'))order by sys_guid())where ROWNUM<=5");
                            DataRow drcalc;
                            foreach (DataRow dr in tb2.Rows)
                            {
                                drcalc = tb.NewRow();
                                drcalc.ItemArray = dr.ItemArray;
                                if (count == 0)
                                    tb.Rows.Add(drcalc);
                                else
                                {
                                    for (int i = 0; i < count; i++)
                                    {
                                        if (drcalc.ItemArray[0].Equals(tb.Rows[i][0]))
                                            break;
                                        if (i + 1 == count)
                                            tb.Rows.Add(drcalc);
                                    }
                                }
                                if (tb.Rows.Count == 5)
                                    break;
                            }
                        }
                        data.FromDataTable(tb);
                        pagejson = SmDataTable.Serialize(data);
                    }
                }
                else if (dbalias == "TKQ")
                {
                    if (session != null)
                    {
                        SmQuery qry = session.CreateSmQuery(dbalias);
                        string sql = "select * from ( select 项目名称 as val from 勘查项目登记 where 项目名称 like '" +
                            text + "%' union select 许可证号 as val  from 勘查项目登记 where 许可证号 like '" + text + "%' union select " +
                            "项目名称 as val  from 勘查项目登记 where 项目名称 like '" + text.ToUpper() + "%' union select 许可证号 as val  from " +
                            "勘查项目登记 where 许可证号 like '" + text.ToUpper() + "%' union select 项目名称 as val from 勘查项目登记 where 项目名称 like '" +
                            text.ToLower() + "%' union select 许可证号 as val  from 勘查项目登记 where 许可证号 like '" + text.ToLower() +
                            "%') dt where ROWNUM<=5";
                        DataTable tb = qry.GetDataTableBySQL(sql);
                        int count = tb.Rows.Count;
                        if (count != 5)
                        {
                            DataTable tb2 = qry.GetDataTableBySQL("select * from ( select 项目名称 as val from 勘查项目登记 where 项目名称 like '%" +
                            text + "%' union select 许可证号 as val  from 勘查项目登记 where 许可证号 like '%" + text + "%' union select " +
                            "项目名称 as val  from 勘查项目登记 where 项目名称 like '%" + text.ToUpper() + "%' union select 许可证号 as val  from " +
                            "勘查项目登记 where 许可证号 like '%" + text.ToUpper() + "%' union select 项目名称 as val from 勘查项目登记 where 项目名称 like '%" +
                            text.ToLower() + "%' union select 许可证号 as val  from 勘查项目登记 where 许可证号 like '%" + text.ToLower() +
                            "%') dt where ROWNUM<=5");
                            DataRow drcalc;
                            foreach (DataRow dr in tb2.Rows)
                            {
                                drcalc = tb.NewRow();
                                drcalc.ItemArray = dr.ItemArray;
                                tb.Rows.Add(drcalc);
                                if (tb.Rows.Count == 5)
                                    break;
                            }
                        }
                        data.FromDataTable(tb);
                        pagejson = SmDataTable.Serialize(data);
                    }
                }
                else if (dbalias == "MapKS")  //地图矿山查询联想
                {
                    if (session != null)
                    {
                        SmQuery qry = session.CreateSmQuery("CKQDJK");
                        string sql = "select * from ( select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '" +
                            text + "%'union select 矿山名称 as val  from 采矿申请登记 where 矿山名称 like '" +
                            text.ToUpper() + "%' union select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '" +
                            text.ToLower() + "%') dt where ROWNUM<=5";
                        DataTable tb = qry.GetDataTableBySQL(sql);
                        int count = tb.Rows.Count;
                        if (count != 5)
                        {
                            DataTable tb2 = qry.GetDataTableBySQL("select * from ( select 矿山名称 as val from 采矿申请登记 where 矿山名称 like '%" +
                            text + "%' union select 矿山名称 as val  from 采矿申请登记 where 矿山名称 like '%" +
                            text.ToUpper() + "%' union select 矿山名称 as val from 采矿申请登记 where ksmc like '%" +
                            text.ToLower() + "%') dt where ROWNUM<=5");
                            DataRow drcalc;
                            foreach (DataRow dr in tb2.Rows)
                            {
                                drcalc = tb.NewRow();
                                drcalc.ItemArray = dr.ItemArray;
                                tb.Rows.Add(drcalc);
                                if (tb.Rows.Count == 5)
                                    break;
                            }
                        }
                        data.FromDataTable(tb);
                        pagejson = SmDataTable.Serialize(data);
                    }
                }
                else if (dbalias == "MapXZQ")  //地图行政区查询联想
                {
                    if (session != null)
                    {
                        SmQuery qry = session.CreateSmQuery("TDLY");
                        string sql = "select * from ( select XZQMC as val from LYK_CJXZQ where XZQMC like '" +
                             text + "%' union select XZQMC as val  from LYK_QXXZQ where XZQMC like '" + text + "%' union select " +
                             "XZQMC as val  from LYK_XZXZQ where XZQMC like '" + text + "%') dt where ROWNUM<=5";
                        DataTable tb = qry.GetDataTableBySQL(sql);
                        int count = tb.Rows.Count;
                        if (count != 5)
                        {
                            DataTable tb2 = qry.GetDataTableBySQL("select * from ( select XZQMC as val from LYK_CJXZQ where XZQMC like '%" +
                            text + "%' union select XZQMC as val  from LYK_QXXZQ where XZQMC like '%" + text + "%' union select " +
                            "XZQMC as val  from LYK_XZXZQ where XZQMC like '%" + text + "%') dt where ROWNUM<=5");
                            DataRow drcalc;
                            foreach (DataRow dr in tb2.Rows)
                            {
                                drcalc = tb.NewRow();
                                drcalc.ItemArray = dr.ItemArray;
                                tb.Rows.Add(drcalc);
                                if (tb.Rows.Count == 5)
                                    break;
                            }
                        }
                        data.FromDataTable(tb);
                        pagejson = SmDataTable.Serialize(data);
                    }
                }
                else if (dbalias == "MapDZD")  //地图地灾点查询联想
                {
                    if (session != null)
                    {
                        SmQuery qry = session.CreateSmQuery("DZHJ");
                        string sql = "select 县市调查统一编号 as val from DZK_DZZHD where 县市调查统一编号 like '" +
                             text + "%' and ROWNUM<=5";
                        DataTable tb = qry.GetDataTableBySQL(sql);
                        int count = tb.Rows.Count;
                        if (count != 5)
                        {
                            DataTable tb2 = qry.GetDataTableBySQL("select 县市调查统一编号 as val from DZK_DZZHD where 县市调查统一编号 like '%" +
                             text + "%' and ROWNUM<=5");
                            DataRow drcalc;
                            foreach (DataRow dr in tb2.Rows)
                            {
                                drcalc = tb.NewRow();
                                drcalc.ItemArray = dr.ItemArray;
                                tb.Rows.Add(drcalc);
                                if (tb.Rows.Count == 5)
                                    break;
                            }
                        }
                        data.FromDataTable(tb);
                        pagejson = SmDataTable.Serialize(data);
                    }
                }
                return pagejson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        [WebMethod(EnableSession = true, Description = "读取系统数据字典")]
        public string GetMetaConstList(string types)
        {
            SmConstClsList lst = new SmConstClsList();
            if (types != "")
            {
                string[] tps = types.Split(',');
                for (int i = 0; i < tps.Length; i++)
                {
                    SmConstCls cls = new SmConstCls();
                    string tp = tps[i];
                    string dbname = "META";
                    if (tp.Contains("@"))
                    {
                        try
                        {
                            string[] tpp = tp.Split('@'); tp = tpp[0]; dbname = tpp[1];
                            Type t = this.GetType();
                            SmConstCls obj = t.InvokeMember(dbname, System.Reflection.BindingFlags.InvokeMethod, null, this, null) as SmConstCls;
                            lst.ConstClsList.Add(obj);
                        }
                        catch { }

                    }
                    else
                    {
                        ISession session = Session["session"] as ISession;
                        SmQuery qry = session.CreateSmQuery(dbname);
                        DataRowCollection drc = qry.GetDataRowsBySQL("select ConstSLSID, ConstClsType,ConstClsName from META_CONSTCLS where CONSTCLSTYPE='" + tp + "'");
                        if (drc != null && drc.Count > 0)
                        {
                            cls.Type = tp.ToUpper();
                            cls.Name = SmObjectData.TranslateObjectToString(drc[0]["ConstClsName"]);
                            SmConst kong = new SmConst();
                            kong.Key = "";
                            kong.Value = "";
                            cls.ConstList.Add(kong);
                            int id = SmObjectData.TranslateObjectToInt(drc[0]["ConstSLSID"]);
                            DataRowCollection drc2 = qry.GetDataRowsBySQL("select ConstValue,ConstTrans from META_CONST where ConstSLSID=" + id + " order by ConstValue");
                            if (drc2 != null && drc2.Count > 0)
                            {
                                for (int j = 0; j < drc2.Count; j++)
                                {
                                    SmConst cn = new SmConst();
                                    cn.Key = SmObjectData.TranslateObjectToString(drc2[j]["ConstValue"]);
                                    cn.Value = SmObjectData.TranslateObjectToString(drc2[j]["ConstTrans"]);
                                    cls.ConstList.Add(cn);
                                }
                            }
                        }
                        lst.ConstClsList.Add(cls);
                    }
                }
            }
            return SmConstClsList.Serialize(lst);
        }

        [WebMethod(EnableSession = true, Description = "读取系统数据字典")]
        public string GetMetaConstList1(string types)
        {
            SmConstClsList lst = new SmConstClsList();
            if (types != "")
            {
                string[] tps = types.Split(',');
                for (int i = 0; i < tps.Length; i++)
                {
                    SmConstCls cls = new SmConstCls();
                    string tp = tps[i];
                    cls.Type = tp.ToUpper();
                    string dbname = "TJK";

                    ISession session = Session["session"] as ISession;
                    SmQuery qry = session.CreateSmQuery(dbname);
                    string strSql = @"select * from kcl_cb01_zh where bh=(
       select bh from kcl_cb01_zh where 1=1 and mc like '" + tps[i] + @"%' and rownum<=1
) order by bnm";
                    DataRowCollection drc = qry.GetDataRowsBySQL(strSql);
                    if (drc != null && drc.Count > 0)
                    {
                        for (int j = 0; j < drc.Count; j++)
                        {
                            SmConst cn = new SmConst();
                            cn.Key = SmObjectData.TranslateObjectToString(drc[j]["BNM"]);
                            cn.Value = SmObjectData.TranslateObjectToString(drc[j]["MC"]);
                            cls.ConstList.Add(cn);
                        }
                    }
                    lst.ConstClsList.Add(cls);
                }
            }
            return SmConstClsList.Serialize(lst);
        }

        [WebMethod(EnableSession = true, Description = "读取系统数据字典,针对表kcl_cb04_kszl，得到主组分名")]
        public string GetMetaConstList2(string types)
        {
            SmConstClsList lst = new SmConstClsList();
            if (types != "")
            {
                SmConstCls cls = new SmConstCls();
                string tp = types;
                cls.Type = tp.ToUpper();
                string dbname = "TJK";

                ISession session = Session["session"] as ISession;
                SmQuery qry = session.CreateSmQuery(dbname);
                string strSql = @"select KCDM,PZ from kcl_cb04_kszl where kcdm='" + types + "' order by kcdm";
                DataRowCollection drc = qry.GetDataRowsBySQL(strSql);
                if (drc != null && drc.Count > 0)
                {
                    SmConst cn1 = new SmConst();
                    cn1.Key = "";
                    cn1.Value = "";
                    cls.ConstList.Add(cn1);
                    for (int j = 0; j < drc.Count; j++)
                    {
                        SmConst cn = new SmConst();
                        cn.Key = SmObjectData.TranslateObjectToString(drc[j]["KCDM"]);
                        cn.Value = SmObjectData.TranslateObjectToString(drc[j]["PZ"]);
                        cls.ConstList.Add(cn);
                    }
                }
                lst.ConstClsList.Add(cls);
            }
            return SmConstClsList.Serialize(lst);
        }

        [WebMethod(EnableSession = true, Description = "读取系统数据字典,针对表kcl_cb04_kszl，得到品味单位。如：%等")]
        public string GetMetaConstList3(string types)
        {
            SmConstClsList lst = new SmConstClsList();
            if (types != "")
            {
                SmConstCls cls = new SmConstCls();
                string tp = types;
                cls.Type = tp.ToUpper();
                string dbname = "TJK";

                ISession session = Session["session"] as ISession;
                SmQuery qry = session.CreateSmQuery(dbname);
                string strSql = "select distinct max(kcdm) kcdm, pzdwma, min(pzdwm) pzdwm from kcl_cb04_kszl where kcdm=" + types + " group by pzdwma order by pzdwma";
                DataRowCollection drc = qry.GetDataRowsBySQL(strSql);
                if (drc != null && drc.Count > 0)
                {
                    SmConst cn1 = new SmConst();
                    cn1.Key = "";
                    cn1.Value = "";
                    cls.ConstList.Add(cn1);
                    for (int j = 0; j < drc.Count; j++)
                    {
                        SmConst cn = new SmConst();
                        cn.Key = SmObjectData.TranslateObjectToString(drc[j]["kcdm"]);
                        cn.Value = SmObjectData.TranslateObjectToString(drc[j]["pzdwm"]);
                        cls.ConstList.Add(cn);
                    }
                }
                lst.ConstClsList.Add(cls);
            }
            return SmConstClsList.Serialize(lst);
        }

        [WebMethod(EnableSession = true, Description = "读取行政区下拉列表")]
        public string GetXZQConstList(string xzqm)
        {
            SmConstCls cls = new SmConstCls();
            if (xzqm != "")
            {
                ISession session = Session["session"] as ISession;
                SmQuery qry = session.CreateSmQuery("META");
                DataRowCollection drc = qry.GetDataRowsBySQL("select XZQDM,XZQMC FROM META_XZQ WHERE XZQDM LIKE '%" + xzqm + "%'");
                if (drc != null && drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        SmConst cn = new SmConst();
                        cn.Key = SmObjectData.TranslateObjectToString(drc[i]["XZQDM"]);
                        cn.Value = SmObjectData.TranslateObjectToString(drc[i]["XZQMC"]);
                        cls.ConstList.Add(cn);
                    }
                }
            }
            return SmConstCls.Serialize(cls);
        }

        [WebMethod(EnableSession = true, Description = "ConstList矿种")]
        public string GetKZConstList(string xzqm)
        {
            SmConstCls cls = new SmConstCls();
            if (xzqm != "")
            {
                ISession session = Session["session"] as ISession;
                SmQuery qry = session.CreateSmQuery("META");
                DataRowCollection drc = qry.GetDataRowsBySQL("select XZQDM,XZQMC FROM META_XZQ WHERE XZQDM LIKE '%" + xzqm + "%'");
                if (drc != null && drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        SmConst cn = new SmConst();
                        cn.Key = SmObjectData.TranslateObjectToString(drc[i]["XZQDM"]);
                        cn.Value = SmObjectData.TranslateObjectToString(drc[i]["XZQMC"]);
                        cls.ConstList.Add(cn);
                    }
                }
            }
            return SmConstCls.Serialize(cls);
        }

        [WebMethod(EnableSession = true, Description = "读取矿种代码和名称")]
        public string GetKZList()
        {
            string strResult = "[";
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmQuery qry = session.CreateSmQuery("META");
                if (qry != null)
                {
                    DataRowCollection drc1 = qry.GetDataRowsBySQL("select KCFLDM ,KCFLMC FROM META_KCFL");
                    DataRowCollection drc2 = qry.GetDataRowsBySQL("select KCFLDM,KCDM,KCMC FROM META_KCDM");
                    if (drc1 != null)
                    {
                        for (int i = 0; i < drc1.Count; i++)
                        {
                            strResult += "{\"id\":\"" + drc1[i]["KCFLDM"].ToString() + "\",\"pId\":\"0\",\"name\":\"" + drc1[i]["KCFLMC"].ToString() + "\"},";
                        }
                    }
                    if (drc2 != null)
                    {
                        for (int i = 0; i < drc2.Count; i++)
                        {
                            strResult += "{\"id\":\"" + drc2[i]["KCDM"].ToString() + "\",\"pId\":\"" + drc2[i]["KCFLDM"].ToString() + "\",\"name\":\"" + drc2[i]["KCMC"].ToString() + "\"},";
                        }
                    }
                    if (strResult.EndsWith(","))
                    {
                        strResult = strResult.Substring(0, strResult.Length - 1);
                    }
                    strResult += "]";
                }
            }
            return strResult;
        }

        [WebMethod(EnableSession = true, Description = "读取行政区列表")]
        public string GetXZQList(string condition)
        {
            string xzqdm = System.Configuration.ConfigurationManager.AppSettings["XZQDM"];
            xzqdm = xzqdm.EndsWith("0000") ? xzqdm.Substring(0, xzqdm.Length - 4) : xzqdm.EndsWith("00") ? xzqdm.Substring(0, xzqdm.Length - 2) : xzqdm;
            string strResult = "[";
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmQuery qry = session.CreateSmQuery("META");
                if (qry != null)
                {
                    int length = xzqdm.Length;
                    string strcondition = "select XZQDM,XZQMC FROM META_XZQ WHERE LEFT(XZQDM," + length.ToString() + ")='" + xzqdm + "'";
                    string driver = session.getDatasource("META").DataDriverType;
                    if (driver == "SuperORM.Driver.OracleClientDriver")
                    {
                        strcondition = "select XZQDM,XZQMC FROM META_XZQ WHERE SUBSTR(XZQDM,0," + length.ToString() + ")='" + xzqdm + "'";
                    }
                    strcondition += condition == "" ? "" : " AND " + condition;
                    if (strcondition != "")
                    {
                        strcondition += " order by xzqdm";
                    }
                    DataRowCollection drc1 = qry.GetDataRowsBySQL(strcondition);
                    if (drc1 != null)
                    {
                        for (int i = 0; i < drc1.Count; i++)
                        {
                            string dm = drc1[i]["XZQDM"].ToString();
                            string mc = drc1[i]["XZQMC"].ToString();
                            if (dm.Length == 6)
                            {
                                if (dm.EndsWith("0000"))
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"0\",\"name\":\"" + mc + "\"},";
                                }
                                else if (dm.EndsWith("00"))
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 2) + "0000\",\"name\":\"" + mc + "\"},";
                                }
                                else
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 4) + "00\",\"name\":\"" + mc + "\"},";
                                }
                            }
                            else if (dm.Length == 9)
                            {
                                strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 6) + "\",\"name\":\"" + mc + "\"},";
                            }
                            else
                            {
                                strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 9) + "\",\"name\":\"" + mc + "\"},";
                            }
                        }
                    }
                    if (strResult.EndsWith(","))
                    {
                        strResult = strResult.Substring(0, strResult.Length - 1);
                    }
                    strResult += "]";
                }
            }
            return strResult;
        }

        [WebMethod(EnableSession = true, Description = "地图查询(行政区)")]
        public string GetMapXZQList(string condition)
        {
            string xzqdm = System.Configuration.ConfigurationManager.AppSettings["XZQDM"];
            xzqdm = xzqdm.EndsWith("0000") ? xzqdm.Substring(0, xzqdm.Length - 4) : xzqdm.EndsWith("00") ? xzqdm.Substring(0, xzqdm.Length - 2) : xzqdm;
            string strResult = "[";
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmQuery qry = session.CreateSmQuery("TDLY");
                if (qry != null)
                {
                    int length = xzqdm.Length;
                    string strcondition = "select * from (select XZQDM,XZQMC FROM LYK_CJXZQ where substr(XZQDM,0," +
                    length.ToString() + ")='" + xzqdm + "' and " + condition + "union select XZQDM,XZQMC FROM LYK_QXXZQ where substr(XZQDM,0," +
                    length.ToString() + ")='" + xzqdm + "' and " + condition + "union select XZQDM,XZQMC FROM LYK_XZXZQ where substr(XZQDM,0," +
                    length.ToString() + ")='" + xzqdm + "' and " + condition + ")";
                    DataRowCollection drc1 = qry.GetDataRowsBySQL(strcondition);
                    if (drc1 != null)
                    {
                        for (int i = 0; i < drc1.Count; i++)
                        {
                            string dm = drc1[i]["XZQDM"].ToString();
                            string mc = drc1[i]["XZQMC"].ToString();
                            if (dm.Length == 6)
                            {
                                if (dm.EndsWith("0000"))
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"0\",\"name\":\"" + mc + "\"},";
                                }
                                else if (dm.EndsWith("00"))
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 2) + "0000\",\"name\":\"" + mc + "\"},";
                                }
                                else
                                {
                                    strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 4) + "00\",\"name\":\"" + mc + "\"},";
                                }
                            }
                            else if (dm.Length == 9)
                            {
                                strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 6) + "\",\"name\":\"" + mc + "\"},";
                            }
                            else
                            {
                                strResult += "{\"id\":\"" + dm + "\",\"pId\":\"" + dm.Substring(0, 9) + "\",\"name\":\"" + mc + "\"},";
                            }
                        }
                    }
                    if (strResult.EndsWith(","))
                    {
                        strResult = strResult.Substring(0, strResult.Length - 1);
                    }
                    strResult += "]";
                }
            }
            return strResult;
        }

        [WebMethod(EnableSession = true, Description = "更新SmObject对象(Don't乱改)")]
        public string UpdateSmObject(string json)
        {
            string strresult = "对不起，保存失败!";
            SmObjectData obj = SmObjectData.Deserialize(json);
            Type T = GetSmType(obj.DBAlias + "." + obj.TableName);
            if (T != null)
            {
                object objins = T.Assembly.CreateInstance(T.FullName);
                foreach (string key in obj.Data.Keys)
                {
                    PropertyInfo info = T.GetProperty(key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (info != null)
                    {
                        Type tt = info.PropertyType;
                        object o = null;
                        try { o = Convert.ChangeType(obj.Data[key], info.PropertyType); }
                        catch { }
                        object[] os = new object[] { o };
                        try
                        {
                            T.InvokeMember(key, System.Reflection.BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase, null, objins, os);
                        }
                        catch
                        {
                        }
                    }
                }
                string keykey = "";
                bool bAutoIncrement = GetKey(T, ref keykey);
                if (keykey != "")
                {
                    ISession session = Session["session"] as ISession;
                    if (session != null)
                    {
                        session.Close();
                        bool bExists = Helper.CommonHelper.IsExistsRecordEx(session, obj.DBAlias, obj.TableName, keykey, obj.Data[keykey.ToUpper()].ToString());
                        if (bExists)
                        {
                            session.Update(objins, obj.DBAlias);
                            session.Flush();
                            strresult = "保存成功";
                        }
                        else
                        {
                            int keyid = -1;
                            if (!bAutoIncrement)
                            {
                                keyid = Helper.CommonHelper.GetMaxID(session, obj.DBAlias, obj.TableName, keykey);
                                object[] os = new object[] { keyid };
                                T.InvokeMember(keykey, System.Reflection.BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase, null, objins, os);
                            }
                            session.Save(objins, obj.DBAlias);
                            session.Flush();
                            strresult = "保存成功";
                        }
                    }
                }
            }
            return strresult;
        }

        [WebMethod(EnableSession = true, Description = "更新SmObject对象(Don't乱改)")]
        public string UpdateSmObjectTKQ(string json)
        {
            string strresult = "对不起，保存失败!";
            SmObjectData obj = SmObjectData.Deserialize(json);
            Type T = GetSmType(obj.DBAlias + "." + obj.TableName);
            string strProject_Id = "";
            if (T != null)
            {
                object objins = T.Assembly.CreateInstance(T.FullName);
                foreach (string key in obj.Data.Keys)
                {
                    PropertyInfo info = T.GetProperty(key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (info != null)
                    {
                        if (key.ToUpper() == "PROJECT_ID")
                        {
                            strProject_Id = obj.Data[key].ToString();
                        }
                        Type tt = info.PropertyType;
                        object o = null;
                        try { o = Convert.ChangeType(obj.Data[key], info.PropertyType); }
                        catch { }
                        object[] os = new object[] { o };
                        try
                        {
                            T.InvokeMember(key, System.Reflection.BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase, null, objins, os);
                        }
                        catch { }
                    }
                }
                string keykey = "";
                bool bAutoIncrement = GetKey(T, ref keykey);
                if (keykey != "")
                {
                    ISession session = Session["session"] as ISession;
                    if (session != null)
                    {
                        session.Close();
                        bool bExists = CheckTKQ(session, obj.TableName, " where project_id='" + strProject_Id + "'");
                        if (bExists)
                        {
                            session.Update(objins, obj.DBAlias);
                            session.Flush();
                            strresult = "保存成功";
                        }
                        else
                        {
                            string keyid = GetTKQLicenseCode(session, obj.TableName, strProject_Id, obj.Data["申请序号"].ToString(), "申请序号");
                            if (!bAutoIncrement)
                            {
                                object[] os = new object[] { keyid };
                                T.InvokeMember(keykey, System.Reflection.BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase, null, objins, os);
                            }
                            session.Save(objins, obj.DBAlias);
                            session.Flush();
                            strresult = "保存成功";
                        }
                    }
                }
            }
            return strresult;
        }

        bool CheckTKQ(ISession session, string strTableName, string strCondition)
        {
            bool bexists = false;

            try
            {
                SmQuery qry = session.CreateSmQuery("TKQ");
                DataRowCollection drc = qry.GetDataRowsBySQL("select * from " + strTableName + strCondition);
                if (drc != null && drc.Count > 0)
                    bexists = true;
            }
            catch { }
            return bexists;
        }

        string GetTKQLicenseCode(ISession session, string strTableName, string strProject_Id, string strLicenseCode, string strColumnName)
        {
            if (strLicenseCode == "")
            {
                strLicenseCode = strProject_Id.Replace("-", "");
                strLicenseCode = strLicenseCode.Substring(strLicenseCode.Length - 13, 13);
            }
            string strCondition = " where " + strColumnName + "='" + strLicenseCode + "'";

            SmQuery qry = session.CreateSmQuery("TKQ");
            DataRowCollection drc = qry.GetDataRowsBySQL("select " + strColumnName + " from " + strTableName + strCondition);
            if (drc != null && drc.Count > 0)
            {
                long l = long.Parse(drc[0][0].ToString());
                l++;
                string strL = l.ToString();
                if (strL.Length < 13)
                {
                    while (true)
                    {
                        strL += "0" + strL;
                        if (strL.Length == 13)
                            break;
                    }
                }
                return GetTKQLicenseCode(session, strTableName, strProject_Id, strL, strColumnName);
            }
            else
            {
                return strLicenseCode;
            }
        }

        #region 这是oracle数据库的，暂时用不到，注掉，2014.1.6  刘树峰
        /*  这是oracle数据库的，暂时用不到，注掉，2014.1.6  刘树峰
         * 
        [WebMethod(EnableSession = true, Description = "更新SmObject对象(Don't乱改)")]
        public string UpdateSmObj(string json)
        {
            string strresult = "对不起，保存失败!";
            SmObjectData obj = SmObjectData.Deserialize(json);
            ISession session = Session["session"] as ISession;
            string strConnection = session.Datasources[obj.DBAlias].ConnectionString;
            OracleConnection oConn = new OracleConnection(strConnection);
            OracleCommand ocmd = new OracleCommand();
            bool bIsNotSave = true;
            OracleDataAdapter oda = null;
            DataTable dt = new DataTable();
            string strSql = "";
            bool b = false;
            try
            {
                //判断是否存在
                strSql = "select count(*) from " + obj.TableName + " where ksbh='" + obj.Data["KSBH"] + "'";
                oda = new OracleDataAdapter(strSql, oConn);
                oda.Fill(dt);
            }
            catch
            {
                //判断是否存在
                strSql = "select count(*) from " + obj.TableName + " where ID=" + obj.Data["ID"];
                oda = new OracleDataAdapter(strSql, oConn);
                oda.Fill(dt);
                b = true;
            }
            if (dt != null && Convert.ToInt32(dt.Rows[0][0]) > 0)
            {
                bIsNotSave = false;
            }

            string strColumnName = "";
            string strCondition = "";
            foreach (string key in obj.Data.Keys)
            {
                object objVal = obj.Data[key];
                DateTime dtime;
                try
                {
                    if (DateTime.TryParse(obj.Data[key].ToString(), out dtime))
                    {
                        objVal = dtime;
                    }
                }
                catch { }
                if (bIsNotSave)
                {
                    if (key.ToUpper() == "ID")
                    {
                        objVal = GetSPYJMaxID(oConn, obj.TableName);
                    }
                    strColumnName += ":" + key + ",";
                }
                else
                {
                    if (key.ToUpper() == "KSBH" || key == "ID")
                    {
                        string str = "";
                        if (key == "KSBH")
                            str = "'";
                        strCondition = " where " + key.ToString() + "=" + str + objVal.ToString() + str;
                        continue;
                    }

                    strColumnName += key + "=:" + key + ",";
                }
                ocmd.Parameters.AddWithValue(":" + key, objVal);
            }
            if (!strColumnName.Trim().Equals(string.Empty))
            {
                strColumnName = strColumnName.Remove(strColumnName.Length - 1);
            }

            if (bIsNotSave)
                ocmd.CommandText = "Insert into " + obj.TableName + "(" + strColumnName.Replace(":", "") + ") values(" + strColumnName + ")";
            else if (!strCondition.Trim().Equals(string.Empty))
                ocmd.CommandText = "Update " + obj.TableName + " set " + strColumnName + strCondition;
            ocmd.CommandType = CommandType.Text;
            ocmd.Connection = oConn;
            if (oConn.State == ConnectionState.Closed)
            {
                oConn.Open();
            }
            try
            {
                if (ocmd.ExecuteNonQuery() > 0)
                {
                    strresult = "true";
                }
            }
            catch
            {
                strresult = "false";
            }
            oConn.Close();
            return strresult;
        }
        

        int GetSPYJMaxID(OracleConnection oConn, string strTableName)
        {
            int iId = 1;
            //得到最大ID
            string sql = "select max(ID) from " + strTableName;
            OracleDataAdapter oba = new OracleDataAdapter(sql, oConn);
            DataTable dt = new DataTable();
            oba.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                if (!dt.Rows[0][0].ToString().Trim().Equals(string.Empty))
                    iId = (Convert.ToInt32(dt.Rows[0][0])) + 1;
            }

            return iId;
        }
         */
        #endregion

        [WebMethod(EnableSession = true, Description = "删除SmObject对象 如果删除的表里有DateTime类型的属性 不适用")]
        public string DeleteSmObject(string json)
        {
            string strresult = "对不起，删除失败!";
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmObjectData obj = SmObjectData.Deserialize(json);
                Type T = GetSmType(obj.DBAlias + "." + obj.TableName);
                if (T != null)
                {
                    string keykey = "";
                    bool bAutoIncrement = GetKey(T, ref keykey);
                    if (keykey != "")
                    {
                        bool bExists = Helper.CommonHelper.IsExistsRecordEx(session, obj.DBAlias, obj.TableName, keykey, obj.Data[keykey.ToUpper()].ToString());
                        if (bExists)
                        {
                            session.Delete(T, keykey + "=" + obj.Data[keykey.ToUpper()].ToString());
                            session.Flush();
                            strresult = "删除成功";
                        }
                        else
                        {
                            strresult = "数据库中不存在该记录，不能删除";
                        }
                    }
                    else
                    {
                        strresult = "要删除的表中不存在主键，不能删除！";
                    }
                }
            }
            return strresult;
        }

        [WebMethod(EnableSession = true, Description = "用来测试的")]
        public string forTest(string classname)
        {
            Type t = GetSmType(classname);
            return "";
        }

        Type GetSmType(string classname)
        {
            Type t = null;
            Assembly[] asss = System.Threading.Thread.GetDomain().GetAssemblies();
            if (asss != null && asss.Length > 0)
            {
                for (int i = 0; i < asss.Length; i++)
                {
                    bool b = true;
                    try
                    {
                        b = asss[i].EscapedCodeBase.ToLower().Contains("c:\\windows");
                    }
                    catch { }
                    if (!b)
                    {
                        t = asss[i].GetType(classname);
                        if (t != null)
                        {
                            break;
                        }
                    }
                }
            }
            string keyk = "a";
            bool bb = GetKey(t, ref keyk);
            return t;
        }

        bool GetKey(Type T, ref string keykey)
        {
            PropertyInfo[] infos = T.GetProperties();
            for (int i = 0; i < infos.Length; i++)
            {
                SuperORM.Attributes.MemberAttribute att = infos[i].GetCustomAttributes(typeof(SuperORM.Attributes.MemberAttribute), true)[0] as SuperORM.Attributes.MemberAttribute;
                if (att != null && att.Key)
                {
                    keykey = infos[i].Name.ToUpper();
                    return att.AutoIncrement;
                }
            }
            return false;
        }

        public SmConstCls GetKZMeta()
        {
            SmConstCls cls = new SmConstCls();
            cls.Type = "KZ";
            cls.Name = "矿种";
            SmConst kong = new SmConst();
            kong.Key = "";
            kong.Value = "";
            cls.ConstList.Add(kong);
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmQuery qry = session.CreateSmQuery("META");
                if (qry != null)
                {
                    DataRowCollection drc2 = qry.GetDataRowsBySQL("select KCFLDM,KCDM,KCMC FROM META_KCDM");
                    if (drc2 != null)
                    {
                        for (int i = 0; i < drc2.Count; i++)
                        {
                            SmConst cn = new SmConst();
                            cn.Key = drc2[i]["KCDM"].ToString();
                            cn.Value = drc2[i]["KCMC"].ToString();
                            cls.ConstList.Add(cn);
                        }
                    }
                }
            }
            return cls;
        }

        public static string GetXzqmcByXzqdm(ISession session, string dm)
        {
            string xzqmc = "";
            SmQuery qry = session.CreateSmQuery("META");
            DataRowCollection drc = qry.GetDataRowsBySQL("select XZQMC FROM META_XZQ WHERE XZQDM='" + dm + "'");
            if (drc != null && drc.Count > 0)
            {
                xzqmc = drc[0][0].ToString();
            }
            return xzqmc;
        }
        public static string GetXzqdmByXzqmc(ISession session, string mc)
        {
            string xzqdm = "";
            SmQuery qry = session.CreateSmQuery("META");
            DataRowCollection drc = qry.GetDataRowsBySQL("select XZQDM FROM META_XZQ WHERE  xzqmc like '%" + mc + "%'");
            if (drc != null && drc.Count > 0)
            {
                xzqdm = drc[0][0].ToString();
            }
            return xzqdm;
        }

        /// <summary>
        /// 判断是否已经打印凭证或者是否已经打印许可证
        /// </summary>
        /// <param name="strAlias">数据源别名</param>
        /// <param name="strTableName">表明</param>
        /// <param name="Project_id"></param>
        /// <param name="strPrintCredentials">查询的列</param>
        /// <returns></returns>
        [WebMethod(EnableSession = true, Description = "判断是否已经打印凭证或者是否已经打印许可证")]
        public string ChangePrintType(string strAlias, string strTableName, string Project_id, string strPrintCredentials)
        {
            ISession session = Session["session"] as ISession;
            try
            {
                if (session != null)
                {
                    SmQuery query = session.CreateSmQuery(strAlias);
                    string strSql = "select " + strPrintCredentials + " from " + strTableName + " where project_id='" + Project_id + "'";
                    DataRowCollection drc = query.GetDataRowsBySQL(strSql);
                    if (drc != null && drc.Count > 0 && drc[0][0].ToString().Equals("1"))
                    {
                        return "true";
                    }
                    SmCommand cmd = session.CreateCommand(strAlias);
                    strSql = "Update " + strTableName + " set " + strPrintCredentials + "=1 where project_id='" + Project_id + "'";
                    cmd.CommandText = strSql;
                    cmd.CommandType = CommandType.Text;
                    if (cmd.ExecuteNonQuery() < 0)
                        return "true";
                }
            }
            catch { }
            return "false";
        }

        [WebMethod(EnableSession = true, Description = "获取某个查询条件的字段唯一值列表")]
        public string GetDistinctList(string dbalias, string sql)
        {
            SmConstClsList lst = new SmConstClsList();

            SmConstCls cls = new SmConstCls();
            cls.Type = "distinct";
            ISession session = Session["session"] as ISession;
            if (session != null)
            {
                SmQuery qry = session.CreateSmQuery(dbalias);
                DataRowCollection drc = qry.GetDataRowsBySQL(sql);
                if (drc != null && drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        if (drc[i][0] != DBNull.Value)
                        {
                            SmConst sc = new SmConst();
                            sc.Key = drc[i][0].ToString();
                            sc.Value = drc[i][0].ToString();
                            cls.ConstList.Add(sc);
                        }
                    }
                }
            }

            lst.ConstClsList.Add(cls);

            return SmConstClsList.Serialize(lst);
        }
        [WebMethod(EnableSession = true, Description = "获取记录数")]
        public int GetSmRecordCount(string dbalias, string sql)
        {
            try
            {
                ISession session = Session["session"] as ISession;
                if (session != null)
                {
                    SmQuery qry = session.CreateSmQuery(dbalias);
                    DataRowCollection drc = qry.GetDataRowsBySQL(sql);
                    if (drc != null && drc.Count > 0) return Convert.ToInt32(drc[0][0]);
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
            return 0;
        }

        [WebMethod(EnableSession = true, Description = "cesi")]
        public string CallJavaService()
        {
            string url = "http://localhost:3521/test/CHECKRULES/DataCheckResultex?id=1120131104164232863d2fe126";

            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(url);
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s;
            s = response.GetResponseStream();

            StreamReader readStream = new StreamReader(s, Encoding.UTF8);
            return "sadf";
        }
    }

